/*
 *  $Id: PlayerInfo.cs 1199 2010-11-20 17:38:24Z thenn.erannor $
 *  This file is a part of Ragima CCG.
 *  (C) 2008-2010 Ragima development group.
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;

using AgateLib.DisplayLib;

using Ragima.Languages;

using Nereal.Serialization;

namespace Ragima {
    [UpdateableOnly]
    public sealed class PlayerInfo : INamed {
        private Names _name;
        private string _avatarFileName;
        private string _deckFileName;
        private Surface _avatar;
        private CardDeck _deck;

        public PlayerInfo(string id) {
            _name = new Names(id, true);
        }

        [XmlName("name")]
        public Names Name {
            get { return _name; }
        }

        [XmlName("avatar")]
        public string AvatarFileName {
            get { return _avatarFileName; }
            set {
                _avatarFileName = value;
                if (_avatar != null) {
                    _avatar.Dispose();
                    _avatar = null;
                }
            }
        }

        [NotSerialize]
        public bool HasAvatar {
            get { return !string.IsNullOrEmpty(_avatarFileName); }
        }

        [NotSerialize]
        public FilePath AvatarFullName {
            get { return HasAvatar ? Paths.Avatars[_avatarFileName] : FilePath.Empty; }
        }

        [NotSerialize]
        public bool AvatarExists {
            get { return HasAvatar ? AvatarFullName.Exists : false; }
        }

        [NotSerialize]
        public Surface Avatar {
            get {
                if (_avatar == null && AvatarExists)
                    _avatar = new Surface(AvatarFullName);
                return _avatar;
            }
        }

        [XmlName("deck")]
        public string DeckFileName {
            get { return _deckFileName; }
            set {
                _deckFileName = value;
                _deck = null;
            }
        }

        [NotSerialize]
        public CardDeck Deck {
            get {
                if (_deck == null) {
                    _deck = CardDeck.LoadFrom(_deckFileName);
                    if (_deck == null) _deck = new CardDeck();
                }
                return _deck;
            }
            set { _deck = value != null ? value.Copy() : null; }
        }

        public void ForceDefaultName(Language lang, Names name) {
            if (!_name.HasResource(lang))
                _name.AddResource(name.GetResource(lang));
        }
    }
}
